<?php
/**
 * 路径调试脚本
 * 用于检测文件路径和服务器环境
 */

echo "<h2>路径调试信息</h2>\n";
echo "<p><strong>当前文件位置:</strong> " . __FILE__ . "</p>\n";
echo "<p><strong>当前目录:</strong> " . __DIR__ . "</p>\n";
echo "<p><strong>服务器文档根目录:</strong> " . $_SERVER['DOCUMENT_ROOT'] . "</p>\n";

echo "<h3>可能的路径测试</h3>\n";

$testPaths = [
    '../../utils/Database.php',
    '../../../utils/Database.php', 
    '../../backend/utils/Database.php',
    '../../../backend/utils/Database.php',
    dirname(__DIR__, 2) . '/utils/Database.php',
    dirname(__DIR__, 3) . '/utils/Database.php',
    dirname(__DIR__, 2) . '/backend/utils/Database.php',
    dirname(__DIR__, 3) . '/backend/utils/Database.php',
];

foreach ($testPaths as $path) {
    $realPath = realpath($path);
    $exists = file_exists($path);
    echo "<p><strong>$path:</strong> " . ($exists ? "✓ 存在" : "✗ 不存在");
    if ($realPath) {
        echo " → " . $realPath;
    }
    echo "</p>\n";
}

echo "<h3>目录结构探测</h3>\n";

// 探测上级目录
for ($i = 1; $i <= 5; $i++) {
    $dir = str_repeat('../', $i);
    if (is_dir($dir)) {
        echo "<h4>$dir</h4>\n";
        $files = scandir($dir);
        echo "<ul>\n";
        foreach ($files as $file) {
            if ($file !== '.' && $file !== '..') {
                echo "<li>$file" . (is_dir($dir . $file) ? ' (目录)' : '') . "</li>\n";
            }
        }
        echo "</ul>\n";
    }
}

// 检查具体文件
echo "<h3>关键文件检查</h3>\n";
$keyFiles = [
    'Database.php',
    'database.php',
    'Response.php'
];

function findFiles($dir, $filename, $depth = 0, $maxDepth = 3) {
    $results = [];
    if ($depth > $maxDepth) return $results;
    
    $files = @scandir($dir);
    if (!$files) return $results;
    
    foreach ($files as $file) {
        if ($file === '.' || $file === '..') continue;
        
        $fullPath = $dir . '/' . $file;
        if ($file === $filename) {
            $results[] = $fullPath;
        } elseif (is_dir($fullPath)) {
            $results = array_merge($results, findFiles($fullPath, $filename, $depth + 1, $maxDepth));
        }
    }
    return $results;
}

foreach ($keyFiles as $file) {
    echo "<p><strong>搜索 $file:</strong></p>\n";
    $found = findFiles(dirname(__DIR__, 3), $file);
    if ($found) {
        echo "<ul>\n";
        foreach ($found as $path) {
            echo "<li>$path</li>\n";
        }
        echo "</ul>\n";
    } else {
        echo "<p>未找到</p>\n";
    }
}
?>